/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IO.models;

import Map.Map;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

/**
 *
 * @author insane
 */
public class MapsConfig implements Serializable {

    private static ArrayList<Map> maps = null;
    private static MapsConfig instance;

    public static MapsConfig getInstance() throws FileNotFoundException, IOException, ClassNotFoundException {
        if (instance == null) {
            instance = new MapsConfig();
        }
        return instance;
    }

    private MapsConfig() throws FileNotFoundException, IOException, ClassNotFoundException {
        load();
    }

    public ArrayList<Map> getMap() {
        return this.maps;
    }

    public void save() throws FileNotFoundException, IOException {
        FileOutputStream fos = new FileOutputStream("maps");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(maps);
        oos.flush();
        oos.close();
    }

    public void load() throws FileNotFoundException, IOException, ClassNotFoundException {
        File f = new File("maps");
        if (f.exists()) {
            FileInputStream fis = new FileInputStream("maps");
            ObjectInputStream oin = new ObjectInputStream(fis);
            maps = (ArrayList) oin.readObject();
        } else {
            maps = new ArrayList<Map>();

            maps.add(new Map(0, 25, 25, 2.9, 20, 20));
            maps.add(new Map(1, 600, 600, 3.9, 20, 20));
            save();
        }
    }
}
